package com.mall.seckill.controller;

import com.github.pagehelper.PageInfo;
import com.mall.common.annotation.IngoreResponseAdvice;
import com.mall.common.response.CommonResponse;
import com.mall.constant.enums.CommonStatus;
import com.mall.constant.strings.SuccessMsg;
import com.mall.entity.TbGoodsEvaluate;
import com.mall.entity.TbSeckillItem;
import com.mall.seckill.service.ISeckillItemService;
import com.mall.seckill.entity.vo.SeckillItemVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * created with IntelliJ IDEA
 *
 * @author: W
 * @date: 2021/07/18/9:10
 * Other:  秒杀活动控制层
 */
@RestController
@RequestMapping("/seckillItem")
@CrossOrigin
public class SeckillItemController {

    @Autowired
    private ISeckillItemService seckillItemService;
    /**
     * 查询秒杀商品 条件查询 状态  配置名称 商品id/名称
     *
     * @param seckillItem 秒杀商品
     * @return {@link PageInfo}
     */
    @PostMapping("/findAll")
    public PageInfo findAll(@RequestBody TbSeckillItem seckillItem,
                            @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
        return seckillItemService.findAll(seckillItem,pageNum,pageSize);
    }

    @GetMapping("/findById")
    @IngoreResponseAdvice
    public TbSeckillItem findById(Long id){
        return seckillItemService.findById(id);
    }


    /**
     *     获取秒杀商品信息
     */
    @RequestMapping("/findSeckillList")
    public List<SeckillItemVo> findSeckillList(String time){
        return seckillItemService.findSeckillList(time);
    }

    @GetMapping("/addSeckillToRedis")
    public CommonResponse addSeckillToRedis(){
        seckillItemService.addSeckillToRedis();
        return new CommonResponse<>(CommonStatus.VALID.getCode(), SuccessMsg.SUCCESS_CODE_200);
    }

    /**
     * App端 限时秒杀页面后端接口
     *
     * @param uid 用户id
     * @param id 秒杀商品id
     * @return CommonResponse
     * 杀方法
     * 需要传入当前登录人和商品编号
     */
    @RequestMapping("/doSeckill")
    public CommonResponse doSeckill(@RequestParam("uid") Long uid, @RequestParam("id") Long id){
        return seckillItemService.doSeckill(uid,id);
    }

    /**
     *     查看商品评论
     */
    @RequestMapping("/findGoodsEvaluate")
    public List<TbGoodsEvaluate> findGoodsEvaluate(Integer id){
        return seckillItemService.findGoodsEvaluate(id);
    }

    /**
     * 加入购物车
     * @param uid
     * @param id
     * @return
     */
    @RequestMapping("/addCar")
    public CommonResponse addCar(Integer uid,Integer id){
        return seckillItemService.addCar(uid,id);
    }


    @PostMapping("/addSeckillItem")
    public Boolean addSeckillItem(@RequestBody SeckillItemVo seckillItemVo){
        return seckillItemService.addSeckillItem(seckillItemVo);
    }


    /**
     * 修改状态
     * @param tbSeckillItem
     * @return
     */
    @PutMapping("/update")
    public Boolean update(@RequestBody TbSeckillItem tbSeckillItem){
        return seckillItemService.update(tbSeckillItem);
    }



}
